class Solution:
    def validUtf8(self, data):
        n_bytes = 0
        for num in data:
            bin_str = bin(num)[2:].zfill(8)
            if n_bytes == 0:
                if bin_str.startswith('0'):
                    continue
                elif bin_str.startswith('110'):
                    n_bytes = 1
                elif bin_str.startswith('1110'):
                    n_bytes = 2
                elif bin_str.startswith('11110'):
                    n_bytes = 3
                else:
                    return False
            else:
                if not bin_str.startswith('10'):
                    return False
                n_bytes -= 1
        return n_bytes == 0
if __name__ == '__main__':
    data = [197, 130, 1]
    print(Solution().validUtf8(data))